package com.entrogy.dao;

import com.entrogy.bean.PayOrderEntity;
import com.entrogy.bean.PayProduct;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface PayMapper {
    @Select("select id,create_time,expiry_date from pay_order where student_id = #{studentId} and product_id = #{productId} and is_success = 1 and is_out_of_date = 1")
    PayOrderEntity getParentPayOrder(@Param("studentId") int studentId, @Param("productId") int productId);

    @Select("select * from pay_product where id = #{productId}")
    PayProduct getPayProductByID(@Param("productId")int productId);

    @Select("SELECT ppp.*,s.student_name from student_information s inner join (select p.*,pp.productName,pp.productDescription as description from pay_order p ,pay_product pp where p.product_id = pp.id and user_id = #{uid} order by p.update_time desc limit #{beginIndex},#{pageSize}) ppp on ppp.student_id = s.id")
    List<PayOrderEntity> getOrdersByUid(@Param("uid") String uid,@Param("beginIndex") int beginIndex,@Param("pageSize") int pageSize);

    @Insert("insert into pay_order(product_id,user_id,total_price,order_id,student_id,create_time,expiry_date) values (#{productId},#{uid},#{totalPrice},#{orderId},#{studentId},now(),date_add(now(), interval 1 YEAR))")
    void insertPayOrder(@Param("productId") int productId, @Param("uid") String uid,@Param("totalPrice") BigDecimal totalPrice, @Param("orderId") String orderId,@Param("studentId") int studentId);

    @Update("update pay_order set is_success = 1 where order_id = #{orderId}")
    void updatePayOrderIsSuccess(@Param("orderId") String orderId);

    @Select("select sellPrice from pay_product where id = #{productId}")
    BigDecimal getProductPriceById(@Param("productId") int productId);

    @Select("select sellPrice from pay_product where id = (select product_id from pay_order where order_id = #{orderId})")
    BigDecimal getProductPriceByOrderId(@Param("orderId") String orderId);

    @Select("select count(*) from pay_order where user_id = #{uid}")
    Integer getPayOrderTotalCount(@Param("uid") String uid);

    @Select("select p.* from pay_order p where is_success = 1 and is_out_of_date = 1")
    List<PayOrderEntity> getPayOrderList();

    @Update("update pay_order set is_out_of_date = 0 where id = #{id}")
    void updateOrderIsOutOfDate(int id);
}
